package com.globallogic.vkulyk.SSHTool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ActionExecutorFactory {
	SocketDataKeeper socketData = null;
	ActionDataKeeper actionData = null;
	private final Logger logger = LoggerFactory
			.getLogger(ActionExecutorFactory.class);
	
	public ActionExecutorFactory(SocketDataKeeper socketData, ActionDataKeeper actionData){
		this.socketData = socketData;
		this.actionData = actionData;
	}
	
	public ActionExecutor defineAndCreateActionExecutor() {
		ActionExecutor actionExecutor = null;
		String host = socketData.getHost();
		int port = socketData.getPort();
		String user = actionData.getUser();
		String actionType = actionData.getType();
		logger.info("Creating action executor for host:'{}' port:'{}' user:'{}' action:'{}'", host,
				port, user, actionType);
		String password = actionData.getPassword();
		if (actionType.equals("execute_command")) {
			String command = ((CommandExecutionDataKeeper) actionData)
					.getCommand();
			actionExecutor = new CommandExecutor(host, port, user, password,
					command);
		} else if (actionType.equals("put") || actionType.equals("get")) {
			String srcFile = ((FileCopyDataKeeper) actionData).getSrcFile();
			String dstDir = ((FileCopyDataKeeper) actionData).getDstDir();
			if (actionType.equals("put")) {
				actionExecutor = new FilePutter(host, port, user, password,
						srcFile, dstDir);
			} else if (actionType.equals("get")) {
				actionExecutor = new FileGetter(host, port, user, password,
						srcFile, dstDir);
			}
		} else if (actionType.equals("execute_script")) {
			String script = ((ScriptExecutionDataKeeper) actionData)
					.getScript();
			actionExecutor = new ScriptExecutor(host, port, user, password,
					script);
		}
		return actionExecutor;
	}
}
